/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package core.comparators;

import core.Program;
import java.util.Comparator;

/**
 *
 * @author Administrator
 */
public class ProgramComparator {

     public static enum PROGRAMCOMPARATORTYPE
    {
        ID,
        NAME,
        DESCRIPTION,
        LINK,
        PROGRAM_ID
    };

        public Comparator getComparator(PROGRAMCOMPARATORTYPE type, boolean ascending)
    {
        switch(type)
        {

            case NAME:
                return new CompareName(ascending);
            case DESCRIPTION:
                return new CompareDescription(ascending);
            case LINK:
                return new CompareLink(ascending);
            case PROGRAM_ID:
                return new CompareCourseId(ascending);
            default:
            case ID:
                return new CompareId(ascending);

        }

    }

    private class CompareName implements Comparator
    {
        boolean ascending;
        CompareName(boolean ascending)
        {
            this.ascending = ascending;
        }
        public int compare(Object o1, Object o2) {
                Program b1 = (Program) o1;
                Program b2 = (Program) o2;

            return ascending ?
                b1.getName().compareTo(b2.getName()) :
                b2.getName().compareTo(b1.getName());
        }
    }

        private class CompareDescription implements Comparator
    {
        boolean ascending;
        CompareDescription(boolean ascending)
        {
            this.ascending = ascending;
        }
        public int compare(Object o1, Object o2) {
                Program b1 = (Program) o1;
                Program b2 = (Program) o2;

            return ascending ?
                b1.getDescription().compareTo(b2.getDescription()) :
                b2.getDescription().compareTo(b1.getDescription());
        }
    }
        private class CompareLink implements Comparator
    {
        boolean ascending;
        CompareLink(boolean ascending)
        {
            this.ascending = ascending;
        }
        public int compare(Object o1, Object o2) {
                Program b1 = (Program) o1;
                Program b2 = (Program) o2;

            return ascending ?
                b1.getWebLink().compareTo(b2.getWebLink()) :
                b2.getWebLink().compareTo(b1.getWebLink());
        }
    }
        private class CompareCourseId implements Comparator
    {
        boolean ascending;
        CompareCourseId(boolean ascending)
        {
            this.ascending = ascending;
        }
        public int compare(Object o1, Object o2) {
                Program b1 = (Program) o1;
                Program b2 = (Program) o2;

            return ascending ?
                b1.getProgramId().compareTo(b2.getProgramId()) :
                b2.getProgramId().compareTo(b1.getProgramId());
        }


    }
            private class CompareId implements Comparator
    {
        boolean ascending;
        CompareId(boolean ascending)
        {
            this.ascending = ascending;
        }
        public int compare(Object o1, Object o2) {
                Program b1 = (Program) o1;
                Program b2 = (Program) o2;

                Long v1 = new Long(b1.getId());
                Long v2 = new Long(b2.getId());

            return ascending ?
                v1.compareTo(v2) :
                v2.compareTo(v1);
        }
    }
}
